package com.springspirit.monitor.cep;

import org.springframework.beans.factory.InitializingBean;

import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.EPRuntime;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.UpdateListener;

public class EventManager implements InitializingBean{
	
	private static final String PACKAGE_NAME = "com.springspirit.framework.message";
	
	Configuration configuration = null;
	EPServiceProvider epServiceProvider = null;
	EPStatement epStatement = null;
	
	private String expression;
	
	private UpdateListener updateListener;
	
	public void setExpression(String expression){
		this.expression = expression;
	}
	
	private String getExpression(){
		return this.expression;
	}
	
	public void setUpdateListener(UpdateListener updateListener){
		this.updateListener = updateListener;
	}
	
	private UpdateListener getUpdateListener(){
		return this.updateListener;
	}
	
	public EPRuntime getCepEngineRuntime(){
		// Event 생성
		return epServiceProvider.getEPRuntime();
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		configuration = new Configuration();
		configuration.addEventTypeAutoName(PACKAGE_NAME);
		epServiceProvider = EPServiceProviderManager.getDefaultProvider(configuration);

		String expression = getExpression();
		epStatement = epServiceProvider.getEPAdministrator().createEPL(expression);

		epStatement.addListener(getUpdateListener());		
	}
	
}
